1051D - Bicolorings - CodeForces Solution


bitmasks dp *1700

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) x.begin(), x.end()
#define pb push_back
#define pf push_front
#define bit(n, x) ((n >> x) & 1)

using namespace std;
double begintime, endtime;
inline void CALC_TIME()
{
    #ifndef ONLINE_JUDGE
    endtime = clock();
    cerr << "\nexecution time : " << (endtime - begintime + 1) / 1000 << " s\n";
    #endif
}
inline void file(string NAME)
{
    #ifndef ONLINE_JUDGE
    freopen((NAME + ".INP").c_str(), "r", stdin);
    freopen((NAME + ".OUT").c_str(), "w", stdout);
    #endif
}
const int MOD = 998244353;
const int maxN = 1e6 + 68;
const int INF = 1e9;
int n, k;
ll dp[1005][1 << 3][2005];
ll dq(int i, int mask, int comp)
{
    if (i == n - 1) return dp[i][mask][comp] = (comp == k);
    if (dp[i][mask][comp] != -1) return dp[i][mask][comp];
    ll res = 0;
    for (int next_mask = 0; next_mask < (1 << 2); next_mask++) {
        if ((mask ^ next_mask) == 0) res += dq(i + 1, next_mask, comp);
        else if ((mask ^ next_mask) == 3) {
            if (mask == 0 && next_mask == 3) res += dq(i + 1, next_mask, comp + 1);
            else if (mask == 3 && next_mask == 0) res += dq(i + 1, next_mask, comp + 1);
            else if (mask == 1 && next_mask == 2) res += dq(i + 1, next_mask, comp + 2);
            else if (mask == 2 && next_mask == 1) res += dq(i + 1, next_mask, comp + 2);
        }
        else if ((mask ^ next_mask) == 1) {
            if (mask == 0 && next_mask == 1) res += dq(i + 1, next_mask, comp + 1);
            else if (mask == 1 && next_mask == 0) res += dq(i + 1, next_mask, comp);
            else if (mask == 2 && next_mask == 3) res += dq(i + 1, next_mask, comp);
            else if (mask == 3 && next_mask == 2) res += dq(i + 1, next_mask, comp + 1);
        }
        else if ((mask ^ next_mask) == 2) {
            if (mask == 0 && next_mask == 2) res += dq(i + 1, next_mask, comp + 1);
            else if (mask == 2 && next_mask == 0) res += dq(i + 1, next_mask, comp);
            else if (mask == 1 && next_mask == 3) res += dq(i + 1, next_mask, comp);
            else if (mask == 3 && next_mask == 1) res += dq(i + 1, next_mask, comp + 1);
        }
        res %= MOD;
    }
    res %= MOD;
    return dp[i][mask][comp] = res;
}
string get(int x)
{
    string res = "";
    for (int i = 0; i < 2; ++i) {
        if (bit(x, i)) res += "1";
        else res += "0";
    }
    return res;
}
signed main() {
    begintime = clock();
    IOS
    cin >> n >> k;
    memset(dp, -1, sizeof dp);
    ll ans = 0;
    for (int mask = 0; mask < (1 << 2); mask++) {
        if (bit(mask, 0) != bit(mask, 1)) ans += dq(0 , mask, 2);
        else ans += dq(0 , mask, 1);
        ans %= MOD;
    }
    cout << ans;
    return 0;
}

 			  		  	      					 		 			


Comments

Submit
0 Comments
More Questions

1395A - Boboniu Likes to Color Balls
1637C - Andrew and Stones
1334B - Middle Class
260C - Balls and Boxes
1554A - Cherry
11B - Jumping Jack
716A - Crazy Computer
644A - Parliament of Berland
1657C - Bracket Sequence Deletion
1657B - XY Sequence
1009A - Game Shopping
1657A - Integer Moves
230B - T-primes
630A - Again Twenty Five
1234D - Distinct Characters Queries
1183A - Nearest Interesting Number
1009E - Intercity Travelling
1637B - MEX and Array
224A - Parallelepiped
964A - Splits
1615A - Closing The Gap
4C - Registration System
1321A - Contest for Robots
1451A - Subtract or Divide
1B - Spreadsheet
1177A - Digits Sequence (Easy Edition)
1579A - Casimir's String Solitaire
287B - Pipeline
510A - Fox And Snake
1520B - Ordinary Numbers